//
//  XHDetailViewController.m
//  Big Show
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "XHDetailViewController.h"
#import "WX12TableViewCell.h"
#import "XHDetailXiangViewController.h"
#import "XHNetworkHandle.h"
#import "XH12TableViewCell.h"
#import "MJRefresh.h"

@interface XHDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIImageView *img;

@property (nonatomic, retain) NSMutableDictionary *mDic;// 接收网络数据字典

@property (nonatomic, retain) NSMutableArray *mArr;

@property (nonatomic, retain) UIActivityIndicatorView *activi;

@property (nonatomic, retain) UIView *passView;

@property (nonatomic, assign) NSInteger page;



@end

@implementation XHDetailViewController

- (void)dealloc
{
    [_tableView release];
    [_mArr release];
    [_mDic release];
    [_img release];
    [_activi release];
    [_passView release];
    [super dealloc];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = _myTitle;
    
    self.page = 30;
    
    self.navigationController.navigationBar.translucent = NO;
    [self createImageView];
    [self createTabelView];
    [self creatRefresh];
    [self createPassView];
    [self getData];
    [self creatFootRefresh];
    


}


- (void)createPassView
{
    self.passView = [[UIView alloc] initWithFrame:self.view.bounds];
    _passView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.97];
    [self.view addSubview:_passView];
    [_passView release];
    
    self.activi = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _activi.center = _passView.center;
    //    _activi.backgroundColor = [UIColor yellowColor];
    _activi.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [_passView addSubview:_activi];
    
    [_activi startAnimating];
    
    [_activi release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 180, 40)];
    label.text = @"      玩命加载中...";
    //    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [_activi addSubview:label];
    [label release];
}







// 下拉刷新
-(void)creatRefresh
{
    
    [self.mArr removeAllObjects];
    
    [self.tableView addHeaderWithCallback:^{
       
    [self getData];
        
    [self.tableView headerEndRefreshing];
        
    }];
    
}
- (void)createImageView
{
    self.img = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _img.image = [UIImage imageNamed:@"u=2916742164,3184761356&fm=21&gp=0.jpg"];
    [self.view addSubview:_img];
    [_img release];
}





// 获取网络数据
-(void)getData
{
    NSString *docid = [[NSString stringWithFormat:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=30&channel_id=%@&cstart=0&cv=1.1.1&distribution=com.apple.appstore&fields=title&fields=url&fields=source&fields=date&fields=image&fields=image_urls&fields=comment_count&fields=like&infinite=true&net=wifi&platform=0&refresh=1&version=010904", _docid] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [XHNetworkHandle getDataWithURL:docid cookie:@"JSESSIONID=7tFm_0dR9sFG5Bm0IAnlqw" completion:^(id result) {
    self.mDic = result;
    self.mArr = [self.mDic objectForKey:@"result"];
    [_tableView reloadData];
    }];

    [_passView removeFromSuperview];
    [_activi stopAnimating];
    
}

// 创建tableView
-(void)createTabelView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64 ) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 150;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    
}

#pragma mark - 必须实现方法1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mArr.count;
}

#pragma mark - 必须实现方法2
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *arr = [[_mArr objectAtIndex:indexPath.row] objectForKey:@"image_urls"];
    
    if (arr.count < 3) {
        XH12TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) {
            cell = [[[XH12TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
            cell.backgroundColor = [UIColor clearColor];
        }
        [cell setDic:[_mArr objectAtIndex:indexPath.row]];
        return cell;
        
    }else{
        WX12TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[WX12TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.backgroundColor = [UIColor clearColor];
        }
        
        [cell setDic: [_mArr objectAtIndex:indexPath.row]];
        return cell;
    }
    
}
#pragma mark - 点击响应方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    XHDetailXiangViewController *detail12VC = [XHDetailXiangViewController alloc];
    // 取消选中阴影效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    detail12VC.myDic = [_mArr objectAtIndex:indexPath.row];
    detail12VC.docid = [[_mArr objectAtIndex:indexPath.row] objectForKey:@"docid"];
    detail12VC.tt = [[_mArr objectAtIndex:indexPath.row] objectForKey:@"title"];
    detail12VC.urlimage = [[_mArr objectAtIndex:indexPath.row] objectForKey:@"image"];
    
    [self.navigationController pushViewController:detail12VC animated:YES];
}


-(void)creatFootRefresh
{
    
    [self.tableView addFooterWithCallback:^{
        
        NSString *url = [NSString stringWithFormat:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=%ld&channel_id=%@&cstart=%ld&cv=1.1.1&distribution=com.apple.appstore&fields=title&fields=url&fields=source&fields=date&fields=image&fields=image_urls&fields=comment_count&fields=like&infinite=true&net=wifi&platform=0&refresh=1&version=010904",self.page + 30, _docid, self.page];
        self.page += 30;
        
        [XHNetworkHandle getDataWithURL:url cookie:@"JSESSIONID=7tFm_0dR9sFG5Bm0IAnlqw" completion:^(id result) {
            // result为请求接口的返回数据
            self.mDic = result;
            NSArray *arr = [_mDic objectForKey:@"result"];
            
            for (NSDictionary *dic in arr) {
                [self.mArr addObject:dic];
            }
            
            [_tableView reloadData];
            
            [self.tableView footerEndRefreshing];
        }];
        
        
        
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


